//
//  LLWelcomeViewController.swift
//  LLWeibo09
//
//  Created by 林 on 16/3/30.
//  Copyright © 2016年 lin. All rights reserved.
//

import UIKit

import SDWebImage

class LLWelcomeViewController: UIViewController {
    
    // MARK: - 属性
    /// 头像底部约束
    var iconViewBottomCons: NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        prepareUI()
        
        // 在发送网络请求之前,就先使用本地图片,如果有就是用本地图像,没有就使用默认的
        if let urlString = LLUserAccountViewModel.shareInstance.userAccount?.avatar_large {
            let url = NSURL(string: urlString)!
            self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
        }
        
        // 获取用户的头像等信息,显示头像,需要等网络请求完了才加载 
        LLUserAccountViewModel.shareInstance.loadUserInfo { (error) -> () in
            if error != nil {
                print("请求用户信息出错了: \(error)")
                return
            }
            
            // 没有出错,给头像赋值,即使图片在本地有缓存也需要等到网络请求完了才设置
            if let urlString = LLUserAccountViewModel.shareInstance.userAccount?.avatar_large {
                let url = NSURL(string: urlString)!
                self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
            }
        }
        
    }
    
    private func prepareUI() {
        // 1.添加子控件 
        view.addSubview(bkgImageView)
        view.addSubview(iconView)
        view.addSubview(welcomeLabel)
        
        // 2.添加约束
        bkgImageView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        welcomeLabel.translatesAutoresizingMaskIntoConstraints = false
        
        // 背景
        bkgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self.view)
        }
        
        // 头像
        // centerX
        iconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.view)
            make.bottom.equalTo(self.view).offset(-160)
            make.size.equalTo(CGSize(width: 85, height: 85))
        }
        
        // 欢迎label
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }

    }
    
    // 界面已经显示了
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        moveAnimation()
    }
    
    /// 头像移动的动画
    private func moveAnimation() {
        // 动画
        // usingSpringWithDamping: 弹簧名明显程度, 0 - 1, 数值越小,弹簧效果越明显
        // initialSpringVelocity: 初速度
        UIView.animateWithDuration(1, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
//            self.iconViewBottomCons?.constant = -(UIScreen.mainScreen().bounds.height - 160)
            // snp_updateConstraints: 更新约束
            self.iconView.snp_updateConstraints(closure: { (make) -> Void in
                // 更新约束不能修改参照,只能改变constant (offset)
                make.bottom.equalTo(self.view).offset(-(UIScreen.mainScreen().bounds.height - 160))
            })
            
            self.view.layoutIfNeeded()
            }) { (_) -> Void in
                UIView.animateWithDuration(LLDefaultDuration, animations: { () -> Void in
                    self.welcomeLabel.alpha = 1
                    }, completion: { (_) -> Void in
                        print("动画完成,切换界面")
                        // 切换界面
                        AppDelegate.outSwitchRootViewController(LLMainViewController())
                })
        }
    }
    
    // MARK: - 懒加载 
    /// 背景
    private lazy var bkgImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var iconView: UIImageView =  {
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        // 设置圆角
        imageView.layer.cornerRadius = 42.5
        imageView.layer.masksToBounds = true
        
        return imageView
    }()
    
    private lazy var welcomeLabel: UILabel = {
       let label = UILabel()
        
        label.text = "欢迎归来"
        label.font = UIFont.systemFontOfSize(14)
        label.sizeToFit()
        
        label.alpha = 0
        
        return label
        
    }()
    
}
